package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.project.common.Result;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Token;
import edu.ynu.se.xiecheng.achitectureclass.project.service.CameraTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/camera-token")
public class CameraTokenController {

    @Autowired
    private CameraTokenService cameraTokenService;

    /**
     * 获取并保存摄像头的 token
     *
     * @return 保存的 Token 实体
     */
    @PostMapping("/refresh")
    public Result<Token> refreshCameraToken() {
        try {
            Token token = cameraTokenService.refreshAndSaveCameraToken();
            return Result.success(token);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }
}